<script setup lang="ts">
import { toFormValidator } from "@vee-validate/zod";
import { Field, ErrorMessage, useForm } from "vee-validate";
import { AuthAPI } from "@/api/AuthAPI";
import { getCurrentInstance } from "vue";
import * as zod from "zod";
import { AxiosError } from "axios";
import useCountdown from "@/logics/useCountdown";
import { useUserStore } from "@/stores/userStore";

// 创建表单验证规则
const validationSchema = toFormValidator(
  zod.object({
    mobile: zod
      .string({ required_error: "请输入手机号" })
      .regex(/^1[3-9]\d{9}$/, "请输入正确的手机号"),
    code: zod
      .string({ required_error: "请输入验证码" })
      .regex(/^\d{6}$/, "验证码格式错误"),
    isAgree: zod.literal(true, {
      errorMap: () => ({ message: "请勾选同意用户协议" }),
    }),
  })
);

const $ = getCurrentInstance();
const userStore = useUserStore();
// 获取倒计时逻辑
const { start, isActive, count } = useCountdown();

// 发送手机验证码
async function sendMsgCode() {
  if (isActive.value) return;
  // 验证是否填写手机号
  let { valid } = await validateField("mobile");
  if (!valid) return;
  try {
    start(60);
    await AuthAPI.sendMsgCodeOfMobileLogin(values.mobile);
    $?.proxy?.$message({ type: "success", msg: "手机验证码发送成功" });
  } catch (error) {
    if (error instanceof AxiosError) {
      $?.proxy?.$message({ type: "error", msg: error.response?.data.message });
    } else {
      $?.proxy?.$message({ type: "error", msg: "手机验证码发送失败" });
    }
  }
}

// 创建表单验证对象
const { handleSubmit, values, validateField, setFieldValue } = useForm<{
  mobile: string;
  code: string;
  isAgree: boolean;
}>({
  validationSchema: validationSchema,
  // 表单初始值
  initialValues: {
    mobile: "",
    code: "",
    isAgree: false,
  },
});

// 表单提交
const onSubmit = handleSubmit((formValue) => {
  userStore.login(() =>
    AuthAPI.loginByMobileMsgCode(formValue.mobile, formValue.code)
  );
});
</script>
<template>
  <form @submit="onSubmit">
    <Field name="mobile" as="div" class="form-item" v-slot="{ field }">
      <div class="input">
        <i class="iconfont icon-user"></i>
        <input v-bind="field" type="text" placeholder="请输入手机号" />
      </div>
      <ErrorMessage name="mobile" v-slot="{ message }">
        <div class="error">
          <i class="iconfont icon-warning"></i>{{ message }}
        </div>
      </ErrorMessage>
    </Field>
    <Field name="code" as="div" class="form-item" v-slot="{ field }">
      <div class="input">
        <i class="iconfont icon-code"></i>
        <input v-bind="field" type="password" placeholder="请输入验证码" />
        <span class="code" @click="sendMsgCode">{{
          isActive ? `剩余${count}秒` : "发送验证码"
        }}</span>
      </div>
      <ErrorMessage name="code" v-slot="{ message }">
        <div class="error">
          <i class="iconfont icon-warning"></i> {{ message }}
        </div>
      </ErrorMessage>
    </Field>
    <Field name="isAgree" as="div" class="form-item">
      <div class="agree">
        <XtxCheckbox
          :modelValue="values.isAgree"
          @update:model-value="
            setFieldValue('isAgree', $event);
            validateField('isAgree');
          "
        />
        <span>我已同意</span>
        <a href="javascript:">《隐私条款》</a>
        <span>和</span>
        <a href="javascript:">《服务条款》</a>
      </div>
      <ErrorMessage name="isAgree" v-slot="{ message }">
        <div class="error">
          <i class="iconfont icon-warning"></i> {{ message }}
        </div>
      </ErrorMessage>
    </Field>
    <button type="submit" class="btn">登录</button>
  </form>
</template>
